package com.perfect.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.perfect.base.BaseService;
import com.perfect.base.ResultInfo;
import com.perfect.dao.DiscountConsumerMapper;
import com.perfect.dao.DiscountMapper;
import com.perfect.model.DiscountDataModel;
import com.perfect.model.DiscountModel;
import com.perfect.model.DiscountReport;
import com.perfect.query.DiscountQuery;
import com.perfect.query.DiscountUseQuery;
import com.perfect.utils.AssertUtil;
import com.perfect.vo.DiscountConsumerDO;
import com.perfect.vo.DiscountDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * 优惠券Service
 */
@Service
public class DiscountService extends BaseService<DiscountDO, Integer> {

    @Resource
    private DiscountMapper discountMapper;

    @Resource
    private DiscountConsumerMapper discountConsumerMapper; //优惠券用户Mapper


    /**
     * 多条件查询优惠券
     *
     * @param discountQuery
     * @return
     */
    public Map<String, Object> listDiscountByParams(DiscountQuery discountQuery) {
        Map<String, Object> map = new HashMap<>();
        List<DiscountModel> list = new ArrayList<DiscountModel>();
        list = discountMapper.selectDiscountlistByParams(discountQuery);
        //判断优惠券是否过期
        for (DiscountModel item : list) {
            if (discountMapper.selectByPrimaryKey(item.getId()).getEndTime().compareTo(new Date()) > 0) {
                item.setStatus(0);
            } else {
                item.setStatus(1);
            }
        }
        PageHelper.startPage(discountQuery.getPage(), discountQuery.getLimit());
        PageInfo<DiscountModel> pageInfo = new PageInfo<>(list);
        map.put("code", 0);
        map.put("msg", "suceess");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;

    }

    ;

    /**
     * 查询优惠券类型
     *
     * @return
     */
    public List<Map<String, Object>> listDiscountType() {
        return discountMapper.selectDiscountType();
    }

    /**
     * 查询优惠券详情
     */
    public DiscountDataModel listDiscountDataById(Integer id) {
        DiscountDataModel discountDataModel = new DiscountDataModel();
        discountDataModel = discountMapper.selectDiscountDataById(id);
        //判断优惠券状态
        if (discountMapper.selectByPrimaryKey(id).getEndTime().compareTo(new Date()) > 0) {
            discountDataModel.setStatus(0);
        } else {
            discountDataModel.setStatus(1);
        }
        return discountDataModel;
    }


    /**
     * 优惠券使用详情查询
     *
     * @param discountUseQuery
     * @return
     */
    public Map<String, Object> listDiscountUseByParams(DiscountUseQuery discountUseQuery) {
        AssertUtil.isTrue((discountUseQuery.getDiscountId() == null), "参数异常！");
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(discountUseQuery.getPage(), discountUseQuery.getLimit());
        PageInfo<DiscountConsumerDO> pageInfo = new PageInfo<>(discountConsumerMapper.selectDiscountUseByParams(discountUseQuery));//优惠券使用详情的分页查询
        map.put("code", 0);
        map.put("msg", "suceess");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 查询优惠券使用状态
     *
     * @return
     */
    public List<Map<String, Object>> listDiscountUseStatus() {
        return discountMapper.selectDiscountUseStatus();
    }


    /**
     * 根据id查询优惠券
     *
     * @param id
     * @return
     */
    public DiscountDO listDiscountById(Integer id) {
        return discountMapper.selectByPrimaryKey(id);
    }


    /**
     * 添加优惠券
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveDiscount(DiscountDO discountDO) {
        AssertUtil.isTrue((discountDO.getName() == null || discountDO.getName() == ""), "参数异常！");
        checkParamByDiscountDO(discountDO);//优惠券参数判断
        AssertUtil.isTrue(discountDO.getEndTime().compareTo(discountDO.getStartTime()) < 0, "开始时间不能小于结束时间");
        AssertUtil.isTrue(discountDO.getEnableTime().compareTo(discountDO.getStartTime()) < 0, "领取时间不能小于开始时间");
        AssertUtil.isTrue(discountDO.getEnableTime().compareTo(discountDO.getEndTime()) > 0, "领取时间不能大于结束时间");
        AssertUtil.isTrue(discountDO.getStartTime().compareTo(new Date()) < 0, "开始时间不能小于当前时间");
        AssertUtil.isTrue(discountDO.getEndTime().compareTo(new Date()) < 0, "结束时间不能小于当前时间");
        AssertUtil.isTrue(discountDO.getEnableTime().compareTo(new Date()) < 0, "领取时间不能小于当前时间");
        AssertUtil.isTrue(discountDO.getPerLimit() > discountDO.getCount(), "每人限领不能大于总发行量");
        AssertUtil.isTrue(discountDO.getPerLimit() > discountDO.getAmount(), "使用门槛应低于面额");
        discountDO.setStatus(0);//将优惠券状态设置为未过期
        discountDO.setPublishCount(discountDO.getCount());//优惠券总发行数量
        discountDO.setReceiveCount(0);//初始化优惠券领取数量
        discountDO.setUseCount(0);//初始化优惠券已使用数量
        AssertUtil.isTrue(discountMapper.insertSelective(discountDO) < 1, "添加失败");
    }


    /**
     * 判断参数是否非空
     *
     * @param discountDO
     */
    public void checkParamByDiscountDO(DiscountDO discountDO) {
        AssertUtil.isTrue((StringUtils.isBlank(discountDO.getName())), "优惠券名称不能为空！");
        AssertUtil.isTrue((discountDO.getType() == null), "优惠券类型不能为空！");
        AssertUtil.isTrue((discountDO.getPlatform() == null), "使用平台不能为空！");
        AssertUtil.isTrue((discountDO.getCount() == null), "优惠券发行数量不能为空！");
        AssertUtil.isTrue((discountDO.getAmount() == null), "优惠券面额不能为空！");
        AssertUtil.isTrue(discountDO.getStartTime() == null, "开始时间不能为空！");
        AssertUtil.isTrue(discountDO.getEndTime() == null, "结束时间不能为空！");
        AssertUtil.isTrue(discountDO.getEnableTime() == null, "领取时间不能为空！");
    }


    /**
     * 修改优惠券
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDiscount(DiscountDO discountDO) {
        AssertUtil.isTrue((discountDO.getName() == null || discountDO.getName() == ""), "参数异常！");
        checkParamByDiscountDO(discountDO);//优惠券参数判断
        AssertUtil.isTrue(discountDO.getEndTime().compareTo(discountDO.getStartTime()) < 0, "开始时间不能小于结束时间");
        AssertUtil.isTrue(discountDO.getEnableTime().compareTo(discountDO.getStartTime()) < 0, "领取时间不能小于开始时间");
        AssertUtil.isTrue(discountDO.getEnableTime().compareTo(discountDO.getEndTime()) > 0, "领取时间不能大于结束时间");
        AssertUtil.isTrue(discountDO.getPerLimit() > discountDO.getCount(), "每人限领不能大于总发行量");
        AssertUtil.isTrue(discountMapper.updateByPrimaryKeySelective(discountDO) < 1, "添加失败");
    }


    /**
     * 删除优惠券
     */

    @Transactional(propagation = Propagation.REQUIRED)
    public void removeDiscount(Integer id) {
        DiscountDataModel discountDataModel = new DiscountDataModel();
        discountDataModel = discountMapper.selectDiscountDataById(id);
        AssertUtil.isTrue(discountDataModel.getDisUserCount() > 0, "存在未使用的优惠券，不能删除");
        AssertUtil.isTrue(discountMapper.removeDiscountById(id) < 1, "删除失败");
    }

    /**
     * 查询不同种类的优惠券总数
     * @return
     */
    public Map<String,Object> listDiscountCount(){
        List<String> listName = new ArrayList<>();
        List<Integer> listCount = new ArrayList<>();
        List<DiscountReport> list = new ArrayList<>();
        list = discountMapper.selectDiscountCount();
        for (DiscountReport item: list){
                listName.add(item.getName());
                listCount.add(item.getCount());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("listName",listName);
        map.put("listCount",listCount);
        return map;
    }
}
